package com.cyw.search.service.impl;
import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.cyw.common.core.domain.R;
import com.cyw.common.core.domain.entity.SysUser;
import com.cyw.common.helper.LoginHelper;
import com.cyw.search.entity.user.EsUser;
import com.cyw.search.entity.videos.Videos;
import com.cyw.search.mapper.EsUserMapper;
import com.cyw.search.mapper.VideosMapper;
import com.cyw.search.service.VideosService;
import com.cyw.system.mapper.SysUserMapper;
import com.cyw.system.service.ISysOssService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @Author: 二叔
 * @Date: 2022/11/10/23:02
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideosService {

    @Resource
    private VideosMapper videosMapper;

    @Resource
    private ISysOssService iSysOssService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private EsUserMapper esUserMapper;

    @Override
    public R publishVideo(String geoPoint, String description, MultipartFile multipartFile) {
        String videoUrl = iSysOssService.upload(multipartFile).getUrl();
        SysUser sysUser = sysUserMapper.selectUserById(LoginHelper.getUserId());
        Videos videos = Videos.initPublish(description, videoUrl, geoPoint,sysUser);
        System.out.println(videos);
        Integer insert = videosMapper.insert(videos);
        if (insert > 0){
            return R.ok("短视频发布成功");
        }
        return R.fail("短视频发布失败");
    }

    @Override
    public PageInfo<Videos> searchNearVideos(String geoPoint, Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Videos> wrapper = new LambdaEsQueryWrapper<>();
        GeoPoint centerPoint = new GeoPoint(geoPoint);
        wrapper.geoDistance(Videos::getLocation, 100.0, DistanceUnit.KILOMETERS, centerPoint)
            .orderByDistanceAsc(Videos::getLocation, centerPoint);
        return videosMapper.pageQuery(wrapper,pageNum,pageSize);
    }

    @Override
    public PageInfo<Videos> searchRecommendVideos(Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Videos> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.orderByDesc(Videos::getScore);
        return videosMapper.pageQuery(wrapper,pageNum,pageSize);
    }

    @Override
    public PageInfo<Videos> searchMyFollowVideos(Integer pageNum, Integer pageSize) {
        EsUser esUser = esUserMapper.selectById(LoginHelper.getUserId());
        Set<Long> iLoveSet = esUser.getILoveSet();
        LambdaEsQueryWrapper<Videos> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.in(Videos::getUserId,iLoveSet);
        return videosMapper.pageQuery(wrapper,pageNum,pageSize);
    }

    @Override
    public String loveOrUnLove(Long videoId) {
        Videos videos = videosMapper.selectById(videoId);

        //如果没点过爱心,则点爱心,否则取消点爱心
        if (videos.getLikeUserSet().contains(LoginHelper.getUserId())){
            videos.setLikeCount(videos.getLikeCount()-1);
            Set<Long> likeUserSet = videos.getLikeUserSet();
            likeUserSet.remove(LoginHelper.getUserId());
            videos.setLikeUserSet(likeUserSet);
            videosMapper.insert(videos);
            return "已取消";
        }
        videos.setLikeCount(videos.getLikeCount()+1);
        Set<Long> likeUserSet = videos.getLikeUserSet();
        likeUserSet.add(LoginHelper.getUserId());
        videos.setLikeUserSet(likeUserSet);
        return "已喜欢";
    }

    @Override
    public String deleteVideo(Long videoId) {
        return videosMapper.deleteById(videoId) > 0?"删除成功:":"删除失败";
    }
}
